/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author alfredo
 */
public class DateUtil {

    public static Date stringToDate(String s) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
        c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
        c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));

        return c.getTime();
    }

    public static String dateToString(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String ret = dia(c.get(Calendar.DAY_OF_WEEK)) + " " + c.get(Calendar.DAY_OF_MONTH) + "/" + (c.get(Calendar.MONTH)+1) + "/" + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + " ";
        return ret;
    }

    public static int getMes(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return (c.get(Calendar.MONTH)+1);
    }

    public static int getAnio(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    private static String dia(int dayOfWeek) {
        String ret = "";
        switch (dayOfWeek) {
            case Calendar.SATURDAY:
                ret = "SA";
                break;
            case Calendar.SUNDAY:
                ret = "DO";
                break;
            case Calendar.MONDAY:
                ret = "LU";
                break;
            case Calendar.TUESDAY:
                ret = "MA";
                break;
            case Calendar.WEDNESDAY:
                ret = "MI";
                break;
            case Calendar.THURSDAY:
                ret = "JU";
                break;
            case Calendar.FRIDAY:
                ret = "VI";
                break;

        }
        return ret;
    }
}
